package com.mac.hosp_pre.user.api;

import com.mac.hosp_pre.core.AuthContextHolder;
import com.mac.hosp_pre.except.ResultCodeEnum;
import com.mac.hosp_pre.reply.R;
import com.mac.hosp_pre.user.entity.UserInfo;
import com.mac.hosp_pre.user.model.req.LoginReqDto;
import com.mac.hosp_pre.user.model.req.UserAuthReqDto;
import com.mac.hosp_pre.user.model.resp.UserRespDto;
import com.mac.hosp_pre.user.service.UserInfoService;
import com.mac.hosp_pre.util.JwtUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Tag(name = "门户用户接口")
@RestController
@RequestMapping("/api/user")
public class UserInfoApiController {

    @Autowired
    private UserInfoService userInfoService;

    @Operation(summary = "用户手机号登录")
    @PostMapping("login")
    public R login(@RequestBody LoginReqDto login) {
        Map<String,Object> info = userInfoService.loginUser(login);
        return R.success(info);
    }

    @Operation(summary = "用户认证")
    @PostMapping("/userAuth")
    public R userAuth(@RequestBody UserAuthReqDto userAuth, HttpServletRequest request) {
        //传递两个参数，第一个参数用户id，第二个参数认证数据vo对象
        userInfoService.userAuth(JwtUtil.getUserId(request),userAuth);
        return R.success();
    }

    @Operation(summary = "获取用户id信息")
    @GetMapping("/getUserInfo")
    public R getUserInfo(HttpServletRequest request) {
        Long userId = JwtUtil.getUserId(request);
        UserInfo userInfo = userInfoService.getById(userId);
        if (userInfo == null){
            return R.error(ResultCodeEnum.NOT_EXISTS_ERROR);
        }
        UserRespDto userRespDto = new UserRespDto();
        BeanUtils.copyProperties(userInfo,userRespDto);
        return R.success(userRespDto);
    }
}
